<?php
    include_once("../action/checkAuthenticationAction.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Danh sách sản phẩm đặc biệt</title>
	<?php 
        include("import-css.php");
        include("../util/constant.php");
        include("../util/message.php");
		include_once("../util/authUtil.php");
    ?>
</head>
<body>
	<?php include("header.php");?>
<form id="form" method="post">
	<div class="container-fluid">
		<div class="row-fluid">
			<?php 
                include("menu.php");
                include_once("../service/specialProductService.php");
            ?>
			<div id="content" class="span10">
                <div id="msg" class="center alert"></div>
                <div class="row-fluid sortable">
    				<div class="box span12">
    					<div class="box-content">
    						<table class="table table-striped table-bordered bootstrap-datatable datatable">
                                <thead>
                                    <tr>
                                        <th>STT</th>
                                        <th>Mã</th>
                                        <th>Tên</th>
                                        <th>Trạng thái</th>
                                        <th>Chức năng</th>                                        
                                    </tr>
    							</thead>   
    							<tbody>
                                    <?php
                                        $speProducts = findAllSpecialProducts();
                                        $count = 1;
                                        
                                        while($row = mysql_fetch_array($speProducts)){
                                    ?>
                                        <tr>
                                            <td><?php echo $count;?></td>
                                            <td><?php echo $row["PRO_CODE"];?></td>
                                            <td><?php echo $row["PRO_NAME"];?></td>
                                            <td>
                                                <select name="status" <?php if(authUtil(array("edit special_product"),$perm_array,true)){?>onchange="updateStatus(<?php echo $count++;?>, this.value, <?php echo $row["SPRO_ID"];?>);"<?php }?>>
                                                    <?php
                                                        if($row["SPRO_STATUS"] == 0){
                                                    ?>
                                                            <option value="1">Bật</option>
                                                            <option value="0" selected>Tắt</option>
                                                    <?php        
                                                        } else{
                                                    ?>
                                                            <option value="1" selected>Bật</option>
                                                            <option value="0">Tắt</option>
                                                    <?php
                                                        }
                                                    ?>
                                                </select>
                                            </td>
                                            <td>
                                            	<?php
													if(authUtil($require_perm,$perm_array,true)){
                                                ?>
                                                    <a class="btn btn-info" href="special-product-detail.php?action=<?php echo EDIT_ACTION; ?>&id=<?php echo $row["SPRO_ID"];?>">
                                                        <i class="icon-edit icon-white"></i>  
                                                        Cập nhật                                            
                                                    </a>
                                                <?php
													}
                                                ?>
                                            </td>
                                        <?php
                                            }
                                        ?>
                                        </tr>
    							</tbody>
    						 </table>     
    					</div>
    				</div><!--/span-->
    			</div><!--/row-->
            </div><!-- end of content -->
        </div><!-- end of row-fluid -->
        <?php include("footer.php");?>
    </div><!-- container-fluid -->
    <?php include("import-js.php");?>
    <script>
        function updateStatus(index, status, specProId){
            var dataString = "status=" + status + "&specProId=" + specProId;
            
            $.ajax({
                type: "POST",
                url: "../action/ajax/updateSpecialProductStatusProcess.php",
                data: dataString,
                success: function(data){
                    if(data == <?php echo UPDATE_SUCCESSFULLY;?>){
                        showMessage("<?php echo $messages["editSpecialProductStatusSuccessfully"];?>", "success");
                        var count = 1;
                        $('select[name="status"]').each(function(){
                            if(count != index) $(this).prop("selectedIndex", 1);
                            count++;
                        });
                    } else{
                        showMessage("<?php echo $messages["editSpecialProductStatusFailed"];?>", "error");
                    }
                }
            });
        }
    </script>
</form>
</body>
</html>